package com.luokangtao.order.controller;

import com.luokangtao.common.entity.Result;
import com.luokangtao.order.service.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * 推送新信息
 * 可以再自己的Controller写个方法调用WebSocketServer.sendInfo();
 * @author luokangtao
 * @create 2019-08-02 14:59
 */
@RestController
@RequestMapping("/checkcenter")
@Api(value = "WebSocket控制层",tags = "WebSocket控制层")
public class WebSocketController {

//    //页面请求
//    @GetMapping("/socket/{cid}")
//    @ApiOperation("页面请求")
//    public ModelAndView socket(@PathVariable String cid) {
//        ModelAndView mav=new ModelAndView("/socket");
//        mav.addObject("cid", cid);
//        return mav;
//    }

    //推送数据接口
    @RequestMapping("/socket/push/{cid}")
    @ApiOperation("推送数据接口")
    public Result pushToWeb(@PathVariable String cid, String message) {
        try {
            WebSocketServer.sendInfo(message,cid);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(cid+"#"+e.getMessage());
        }
        return Result.ok(cid);
    }

}
